const { $Message } = require('../../../dist/base/index');
let app = getApp()
let utils = app.utils
Page({

    /**
     * 页面的初始数据
     */
    data: {
        value1Name: '',
        value2Des: '',
        value3Tel: '',
        value4WX: '',
        value5QR: '',
        value6Add:'',
        value7Tag: '',
        fruit: [{
            id: 1,
            name: '全国(全部用户可见)',
        }, {
            id: 2,
            name: '附近(附近用户可见)'
        }],
        tags: [{
            id: 1,
            name: '游戏',
        }, {
            id: 2,
            name: '运动'
        }, {
            id: 3,
            name: '生活'
        }],
        GeoPointlat: 0,
        GeoPointlong: 0,
        isGeoPointShow: '',
        isTagsShow: '',
        isGroupQRcode:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    
    },
    /**
     * 定位
     */
    clickLocation:function(e){
        var that = this
        if (e.detail.value == '全国(全部用户可见)'){
            that.setData({
                value6Add: '全国',
                GeoPointlat: 0,
                GeoPointlong: 0,
                isGeoPointShow: e.detail.value,
            });
        }else{
            wx.chooseLocation({
                success: function (res) {
                    console.log(res)
                    that.setData({
                        value6Add: res.address,
                        GeoPointlat: res.latitude,
                        GeoPointlong: res.longitude,
                        isGeoPointShow: e.detail.value,
                    });
                },
                fail:function(){
                    that.setData({
                        value6Add: '',
                        GeoPointlat: 0,
                        GeoPointlong: 0,
                        isGeoPointShow: '',
                    });
                }
            })
        }
    },
    /**
     * 选择二维码
     */
    clickGroupQRcode:function(){
        var that = this
        if (that.data.value5QR == ''){
            wx.scanCode({
                success: (res) => {
                    console.log(res)
                    that.setData({
                        isGroupQRcode: true,
                        value5QR: res.result
                    });
                },
                fail:() => {
                    that.setData({
                        isGroupQRcode: false,
                        value5QR: ''
                    });
                }
            })
        }else{
            that.setData({
                isGroupQRcode: false,
                value5QR: ''
            });
        }
    },
    /**
     * Tag
     */
    clickTagsCell: function (e) {
        var that = this
        that.setData({
            value7Tag: e.detail.value,
            isTagsShow: e.detail.value,
        });
    },
    

    /**
     * 数据绑定 
     * */
    formContent1: function (e) {
        this.setData({
            value1Name: e.detail.detail.value
        })
    },
    formContent2: function (e) {
        this.setData({
            value2Des: e.detail.detail.value
        })
    },
    formContent3: function (e) {
        this.setData({
            value3Tel: e.detail.detail.value
        })
    },
    formContent4: function (e) {
        this.setData({
            value4WX: e.detail.detail.value
        })
    },

    /**
     * 提交
     */
    clickSubmit: function () {
        var that = this
        console.log(that.data)
        console.log(app.globalData.userInfo)
        if (that.data.value1Name == ''){
            $Message({
                content: '未填写群名称',
                type: 'error'
            });
            return
        }
        if (that.data.value2Des == '') {
            $Message({
                content: '未填写群描述',
                type: 'error'
            });
            return
        }
        if (that.data.value4WX == '') {
            $Message({
                content: '未填写群微信',
                type: 'error'
            });
            return
        }
        if (that.data.value6Add == '') {
            $Message({
                content: '未选择群地址',
                type: 'error'
            });
            return
        }
        if (that.data.value7Tag == '') {
            $Message({
                content: '未选择群类型',
                type: 'error'
            });
            return
        }

        if (app.globalData.limited == 0) {
            $Message({
                content: '用户权限不足',
                type: 'error'
            });
            return
        }

        $Message({
            content: '提交成功',
            type: 'success'
        });
        
        var data = {
            groupName: that.data.value1Name,
            groupDetails: that.data.value2Des,
            groupAdd: that.data.value6Add ,
            groupWx: that.data.value4WX,
            groupTags: [that.data.value7Tag],
            groupAuthorId: app.globalData.userInfo.openid,
            groupAuthor: app.globalData.userInfo,
            GeoPoint: {
                 __type: 'GeoPoint', 
                latitude: that.data.GeoPointlat, 
                longitude: that.data.GeoPointlong,
            }
        }
        app.request.saveGroupInfo(data).then(res => {
            var code = res.code
            if (code == 0){
                $Message({
                    content: '提交成功',
                    type: 'success'
                });
            }else{
                $Message({
                    content: '提交失败',
                    type: 'error'
                });
            }
            
        });
        console.log(data)
    },
})